package com.example.analyzevisio.service;

import com.example.analyzevisio.model.Lane;
import com.example.analyzevisio.model.component.Position;
import com.example.analyzevisio.model.component.Size;
import com.example.analyzevisio.model.lane.LaneData;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.example.analyzevisio.utis.Obtain.*;

//获取泳道元素
public class LaneService {
    private static HashMap<String, Map<String, String>> dataMap;
    private static int laneSum ;
    private static double pinX ;
    private static double pinY ;

    private static HashMap<String, List<Position>> positionHashMap = new HashMap<>();



    public void setDataMap(HashMap<String, Map<String, String>> dataMap){
        this.dataMap = dataMap;
    }

    //为子节点提供坐标原点

    /**
     * 第一个是现坐标，第二个是原坐标
     * @return
     */
    public Map<String, List<Position>> getPosition(){
        return positionHashMap;
    }

    /**
     * 获取泳道元素
     * @param shapeElement
     * @param textNodes
     * @return
     */
    public static Lane getLane(Element shapeElement, NodeList textNodes) {


        double width = 23.5;
        double height = 69.0;

        if (getCellValue(shapeElement, "Width") != null){
            width =  Math.ceil( Double.parseDouble(getCellValue(shapeElement, "Width"))*25.4) / 2;
        }
        if (getCellValue(shapeElement, "Height") != null){
            height = Math.ceil( Double.parseDouble(getCellValue(shapeElement, "Height"))*25.4) / 2;
        }


        Lane lane = new Lane();
        if (laneSum > 0){
            pinX = pinX + 340;
        }else {


            double pinX1 = Math.ceil( Double.parseDouble(getCellValue(shapeElement, "PinX"))*25.4);

            pinX = pinX1 - width;

            double pinY1 = Math.ceil(Double.parseDouble(getCellValue(shapeElement, "PinY")) * 25.4);

            pinY =  pinY1 + 250 + height;
        }
        String shapeID = shapeElement.getAttribute("ID");
        Position position = new Position(
                pinX,
                -250
        );
        ArrayList<Position> positions = new ArrayList<>();

        Position position2 = new Position(
                Math.ceil( Double.parseDouble(getCellValue(shapeElement, "PinX"))*25.4) - width,
                Math.ceil( Double.parseDouble(getCellValue(shapeElement, "PinY"))*25.4) + height
        );
        Position position3 = new Position(
                pinX,
                pinY
        );

        positions.add(position3);
        positions.add(position2);


        positionHashMap.put(shapeID,positions);

        Size size = new Size(
//                Double.parseDouble(getCellValue(shapeElement, "Width"))*25.4+"",
//                Double.parseDouble(getCellValue(shapeElement, "Height"))*25.4+""
                340,
                950
        );
        lane.setPosition(position);
        lane.setSize(size);
        lane.setView("react-shape-view");
        lane.setShape("react-shape");
        String uniqueID = shapeElement.getAttribute("UniqueID");
        String substring = uniqueID.substring(1, uniqueID.length() - 1);
        lane.setId(substring);


        String text = getText(textNodes);
        String aacName = text;
        String aacCode = null;
        if (text != null){
            for (int i = 0; i < text.length(); i++) {
                if (text.charAt(i) == '(' && text.charAt(text.length()-1) == ')' ||
                        text.charAt(i) == '（' && text.charAt(text.length()-1) == '）'
                ){
                    aacName = text.substring(0,i);
                    aacCode = text.substring(i+1,text.length()-1);
                }
            }
        }






        LaneData laneData = new LaneData(
                dataMap.get(shapeID).get("stateId"),
                "lane",
                true,
                "lane",
                aacName,
                aacCode
        );
        lane.setData(laneData);
        lane.setZIndex("-10");


        List<String> matches = getStrings(shapeElement);
        ArrayList<String> arrayList = new ArrayList<>();
        for (String match : matches) {
            if (dataMap.containsKey(match)){
                Map<String, String> stringMap = dataMap.get(match);
                arrayList.add(stringMap.get("UniqueID"));
            }
        }
        lane.setChildren(arrayList);


        laneSum++;

        return lane;
    }
}
